//
//  BIDAddPhotoView.m
//  shandongshangbao
//
//  Created by mal on 14/12/2.
//  Copyright (c) 2014年 cnsoft. All rights reserved.
//

#import "BIDAddPhotoView.h"
#import "BIDViewWithImageAndCloseBtn.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface BIDAddPhotoView()<BIDViewWithImageAndCloseBtnDelegate>
{
    UIButton *_addBtn;
    NSMutableArray *_imgViewArr;
    int row;
    NSTimer *_timer;
    UIView *_attachmentView;
}
@end

@implementation BIDAddPhotoView
@synthesize delegate;


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self initView];
}

- (id)init
{
    self = [super init];
    if(self)
    {
        [self initView];
    }
    return self;
}

- (void)initView
{
    //[self setBackgroundColor:[UIColor lightGrayColor]];
    row = 0;
    _imgViewArr = [[NSMutableArray alloc] init];
    _imgArr = [[NSMutableArray alloc] init];
    //
    _attachmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 8, 80, 80)];
    //
    _addBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _addBtn.frame = CGRectMake(0, 0, 80, 80);
    [_addBtn setBackgroundImage:[UIImage imageNamed:@"addPhoto.png"] forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(addBtnHandler) forControlEvents:UIControlEventTouchUpInside];
    [_attachmentView addSubview:_addBtn];
    [self addSubview:_attachmentView];
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    //self.frame = CGRectMake(0, 0, screenSize.width, 106);
}

- (void)addBtnHandler
{
    //
    [delegate choosePhoto];
}

- (NSMutableArray *)imgArr
{
    [_imgArr removeAllObjects];
    for(BIDViewWithImageAndCloseBtn *tempView in _imgViewArr)
    {
        [_imgArr addObject:tempView.imgView.image];
    }
    return _imgArr;
}

- (void)addPhoto:(UIImage *)img
{
    //[self tellOtherToStopWobble];
//    if(img)
//    {
//        [imgArr addObject:img];
//    }
    CGSize screenSize = self.frame.size;
    CGFloat itemSpacing = 8;
    CGFloat lineSpacing = 8;
    CGFloat leftSpacing = 0;
    CGFloat topSpacing = 8;
    CGFloat bottomSpacing = 8;
    CGSize imgViewSize = CGSizeMake(90, 90);
    CGFloat posX = 0;
    CGFloat posY = 0;
    if(_imgViewArr.count>0)
    {
        UIView *lastView = _imgViewArr[_imgViewArr.count-1];
        posX = CGRectGetMaxX(lastView.frame)+itemSpacing;
        if(posX+imgViewSize.width>screenSize.width)
        {
            posX = leftSpacing;
            posY = CGRectGetMaxY(lastView.frame) + lineSpacing;
        }
        else
        {
            posY = CGRectGetMinY(lastView.frame);
        }
    }
    else
    {
        posX = leftSpacing;
        posY = topSpacing;
    }
    BIDViewWithImageAndCloseBtn *imgView = [[[NSBundle mainBundle] loadNibNamed:@"BIDViewWithImageAndCloseBtn" owner:self options:nil] lastObject];
    imgView.delegate = self;
    imgView.tag = _imgViewArr.count;
    imgView.frame = CGRectMake(posX, posY, imgViewSize.width, imgViewSize.height);
    imgView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGR.numberOfTapsRequired = 1;
    tapGR.numberOfTouchesRequired = 1;
    [imgView addGestureRecognizer:tapGR];
    [_imgViewArr addObject:imgView];
    imgView.image = img;
    [self addSubview:imgView];
    //
    CGRect frame = _attachmentView.frame;
    if(CGRectGetMaxX(imgView.frame)+itemSpacing+CGRectGetWidth(frame)>screenSize.width)
    {
        frame.origin.x = 10;
        frame.origin.y = CGRectGetMaxY(imgView.frame) + lineSpacing + 10;
    }
    else
    {
        frame.origin.x = CGRectGetMaxX(imgView.frame) + itemSpacing + 10;
        frame.origin.y = CGRectGetMinY(imgView.frame) + 10;
    }
    _attachmentView.frame = frame;
    //
    CGRect ownFrame = self.frame;
    ownFrame.size.height = CGRectGetMaxY(_attachmentView.frame)+10 + bottomSpacing;
    self.frame = ownFrame;
    [delegate resizeViewWithFrame:ownFrame];
}

/**
 针对于对原有图片进行修改的情况

 @param imgUrl <#imgUrl description#>
 */
- (void)addPhotoWithImgURL:(NSURL *)imgURL
{
    CGSize screenSize = self.frame.size;
    CGFloat itemSpacing = 8;
    CGFloat lineSpacing = 8;
    CGFloat leftSpacing = 0;
    CGFloat topSpacing = 8;
    CGFloat bottomSpacing = 8;
    CGSize imgViewSize = CGSizeMake(90, 90);
    CGFloat posX = 0;
    CGFloat posY = 0;
    if(_imgViewArr.count>0)
    {
        UIView *lastView = _imgViewArr[_imgViewArr.count-1];
        posX = CGRectGetMaxX(lastView.frame)+itemSpacing;
        if(posX+imgViewSize.width>screenSize.width)
        {
            posX = leftSpacing;
            posY = CGRectGetMaxY(lastView.frame) + lineSpacing;
        }
        else
        {
            posY = CGRectGetMinY(lastView.frame);
        }
    }
    else
    {
        posX = leftSpacing;
        posY = topSpacing;
    }
    BIDViewWithImageAndCloseBtn *combinationView = [[[NSBundle mainBundle] loadNibNamed:@"BIDViewWithImageAndCloseBtn" owner:self options:nil] lastObject];
    combinationView.delegate = self;
    combinationView.tag = _imgViewArr.count;
    combinationView.frame = CGRectMake(posX, posY, imgViewSize.width, imgViewSize.height);
    combinationView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)];
    tapGR.numberOfTapsRequired = 1;
    tapGR.numberOfTouchesRequired = 1;
    [combinationView addGestureRecognizer:tapGR];
    [combinationView.imgView setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    [_imgViewArr addObject:combinationView];

    [self addSubview:combinationView];
    //
    CGRect frame = _attachmentView.frame;
    if(CGRectGetMaxX(combinationView.frame)+itemSpacing+CGRectGetWidth(frame)>screenSize.width)
    {
        frame.origin.x = 10;
        frame.origin.y = CGRectGetMaxY(combinationView.frame) + lineSpacing + 10;
    }
    else
    {
        frame.origin.x = CGRectGetMaxX(combinationView.frame) + itemSpacing + 10;
        frame.origin.y = CGRectGetMinY(combinationView.frame) + 10;
    }
    _attachmentView.frame = frame;
    //
    CGRect ownFrame = self.frame;
    ownFrame.size.height = CGRectGetMaxY(_attachmentView.frame)+10 + bottomSpacing;
    self.frame = ownFrame;
    [delegate resizeViewWithFrame:ownFrame];
}

- (void)tapGestureHandler:(UITapGestureRecognizer*)gr
{
    UIView *view = gr.view;
    [delegate showPhotoAtIndex:view.tag withImgArr:self.imgArr];
}

- (void)animationDidStop:(NSString*)animation finished:(BOOL)finished context:(void*)context
{
    //[imgArr removeObjectAtIndex:[(__bridge NSNumber*)context intValue]];
    [_imgViewArr removeObjectAtIndex:[(__bridge NSNumber*)context intValue]];
    CGRect ownFrame = self.frame;
    ownFrame.size.height = CGRectGetMaxY(_attachmentView.frame) + 10 + 8;
    self.frame = ownFrame;
    [delegate resizeViewWithFrame:ownFrame];
    //[self tellOtherToStopWobble];
}

#pragma mark -BIDViewWithImageAndCloseBtnDelegate
- (void)toDelete:(int)index
{
    NSArray *arr = [[NSArray alloc] initWithArray:_imgViewArr copyItems:YES];
    [UIView beginAnimations:@"" context:(__bridge void *)([NSNumber numberWithInt:index])];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    for(int i=index+1; i<_imgViewArr.count; i++)
    {
        UIView *view1 = _imgViewArr[i];
        UIView *frontView = arr[i-1];
        view1.frame = frontView.frame;
        view1.tag = frontView.tag;
    }
    CGRect frame = _attachmentView.frame;
    UIView *customView = arr[arr.count-1];
    if(self.imgArr.count==1)
    {
        //当imgArr.count==1时，说明图片已经清空了
        frame.origin.x = 0;
        frame.origin.y = 0;
    }
    else
    {
        frame.origin.x = CGRectGetMinX(customView.frame)+10;
        frame.origin.y = CGRectGetMinY(customView.frame)+10;
    }
    _attachmentView.frame = frame;    
    [UIView commitAnimations];
    //
    CGRect ownFrame = self.frame;
    ownFrame.size.height = CGRectGetMaxY(_attachmentView.frame)+10 + 8;
    self.frame = ownFrame;
    [delegate resizeViewWithFrame:ownFrame];
}

- (void)tellOtherToWobble
{
    for(__strong UIView *customView in _imgViewArr)
    {
        [(BIDViewWithImageAndCloseBtn*)customView toWobble];
    }
}
- (void)tellOtherToStopWobble
{
    for(UIView *customView in _imgViewArr)
    {
        [(BIDViewWithImageAndCloseBtn*)customView toStop];
    }
}

@end
